//
//  HLMessageListCell.m
//  YueYue
//
//  Created by hulu001 on 2023/6/7.
//

#import "HLMessageListCell.h"
#import <NIMSDK/NIMSDK.h>
#import "HLPushAttachment.h"

@interface HLMessageListCell ()

@property (nonatomic, strong) UIImageView *topView;
@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UILabel *nicknameLabel;
@property (nonatomic, strong) UILabel *infoLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) QMUILabel *numberLabel;
@property (nonatomic, strong) UIView *lineView;
@end

@implementation HLMessageListCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = UIColorWhite;
        [self setupUI];
    }
    return self;
}


- (void)setupUI
{
    self.topView = [[UIImageView alloc] initWithImage:UIImageMake(@"im_recent_top")];
    [self.contentView addSubview:_topView];
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.mas_equalTo(0);
    }];
    
    
    self.avatarView = [[UIImageView alloc] init];
    _avatarView.layer.cornerRadius = 30;
    _avatarView.layer.masksToBounds = YES;
    _avatarView.image = [HLUIHelper avatarBigPlaceholder];
    [self.contentView addSubview:_avatarView];
    [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(60);
        make.left.mas_equalTo(12);
        make.top.mas_equalTo(12);
        make.bottom.mas_equalTo(-12);
    }];
    
    self.nicknameLabel = [[UILabel alloc] init];
    _nicknameLabel.textColor = HLTextColor1;
    _nicknameLabel.text = @"聊人生阿姨有没有啊";
    _nicknameLabel.font = UIFontMediumMake(15);
    [self.contentView addSubview:_nicknameLabel];
    [_nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_avatarView.mas_right).offset(8);
        make.bottom.equalTo(_avatarView.mas_centerY).offset(-2);
        make.right.mas_equalTo(-62);
    }];
    
    self.infoLabel = [[UILabel alloc] init];
    _infoLabel.font = UIFontMake(12);
    _infoLabel.textColor = HLTextColor2;
    _infoLabel.numberOfLines = 1;
    _infoLabel.text = @"没有愚蠢的问题，只有愚蠢的回答";
    [self.contentView addSubview:_infoLabel];
    [_infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nicknameLabel);
        make.top.equalTo(_avatarView.mas_centerY).offset(2);
        make.right.mas_equalTo(-20);
    }];
    
    self.timeLabel = [[UILabel alloc] init];
    _timeLabel.textColor = HLTextColor3;
    _timeLabel.font = UIFontMake(12);
    _timeLabel.text = @"今天12:12";
    [self.contentView addSubview:_timeLabel];
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.right.mas_equalTo(-12);
    }];
    
    self.numberLabel = [[QMUILabel alloc] qmui_initWithFont:UIFontMake(10) textColor:UIColorWhite];
    _numberLabel.text = @"12";
    _numberLabel.layer.masksToBounds = YES;
    _numberLabel.layer.cornerRadius = 8;
    _numberLabel.contentEdgeInsets = UIEdgeInsetsMake(2, 6, 2, 6);
    _numberLabel.backgroundColor = HLAideColor5;
    [self.contentView addSubview:_numberLabel];
    [_numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-12);
        make.centerY.mas_equalTo(10);
    }];

    self.lineView = [[UIView alloc] init];
    _lineView.backgroundColor = HLLineColor;
    [self.contentView addSubview:_lineView];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    
}

- (void)setModel:(NIMRecentSession *)model
{
    _model = model;
    MJWeakSelf
    [HLUIHelper getUserInfoWithSessionId:model.session.sessionId callback:^(NIMUser *user) {
        if ([user.userId isEqualToString:weakSelf.model.session.sessionId]){
            [weakSelf.avatarView sd_setImageWithURL:[NSURL URLWithString:user.userInfo.thumbAvatarUrl] placeholderImage:[HLUIHelper avatarBigPlaceholder]];
            weakSelf.nicknameLabel.text = user.userInfo.nickName;
        }
    }];
    self.topView.hidden = ([NIMSDK.sharedSDK.chatExtendManager stickTopInfoForSession:model.session] == nil);
    self.infoLabel.text = [self showLastMessage:model.lastMessage];
    
    self.numberLabel.hidden = (model.unreadCount == 0);
    self.numberLabel.text = [NSString stringWithFormat:@"%zd",model.unreadCount];
    self.timeLabel.text = [HLHelper showTime:model.lastMessage.timestamp showDetail:NO];
}

- (NSString *)showLastMessage:(NIMMessage *)message
{
    NSString *msg = message.text;
    switch (message.messageType) {
            
        case NIMMessageTypeText:    // 文本
        {
            NSString *c = message.localExt[@"originContent"];
            if (c) {
                msg = c;
            } else {
                msg = message.text;
            }
            
            break;
        }
        case NIMMessageTypeTip:     // 提示
        {
             msg = message.text;
        }
            break;
            
        case NIMMessageTypeAudio:   // 语音
        {
            msg = message.isOutgoingMsg ? @"[你发送了一条语音]" : @"[你收到了一条语音]";
        }
            break;
            
        case NIMMessageTypeVideo:   // 视频
        {
            msg = message.isOutgoingMsg ? @"[你发送了一条视频]" : @"[你收到了一条视频]";
        }
            break;
        case NIMMessageTypeImage:
        {
            msg = message.isOutgoingMsg ? @"[你发送了一张图片]" : @"[你收到了一张图片]";
        }
            break;
        case NIMMessageTypeCustom:  // 自定义
        {
            NIMCustomObject *object = (NIMCustomObject *)message.messageObject;
            
            if ([object.attachment isKindOfClass:NSClassFromString(@"HLAccostAttachment")]) {
                msg = message.isOutgoingMsg ? @"[搭讪消息]" : @"[搭讪消息]";
            } else if ([object.attachment isKindOfClass:NSClassFromString(@"HLGiftAttachment")]) {
                msg = @"[礼物消息]";
            } else if ([object.attachment isKindOfClass:NSClassFromString(@"HLPushAttachment")]) {
                NIMCustomObject *object = (NIMCustomObject *)message.messageObject;
                HLPushAttachment *attachment = (HLPushAttachment *)object.attachment;
                msg = attachment.messageContent;
            } else {
               msg = @"[自定义消息]";
            }
        }
            break;
        case NIMMessageTypeRtcCallRecord:
        {
            msg = @"[视频消息]";
        }
            break;
        default:
           msg = message.text;
            break;
    }
    return msg;
}

@end
